<template>
  <div class="comment">
    <hm_header title="精彩评论页">
      <template #left>
        <van-icon name="arrow-left" @click="$router.go(-1)" />
      </template>
    </hm_header>
    <div class="item" v-for="item in commentList" :key="item.id">
      <div class="head">
        <img :src="axios.defaults.baseURL + item.user.head_img" alt />
        <div>
          <p>{{item.user.nickname}}</p>
          <span>时间</span>
        </div>
        <span @click="replayComment(item)">回复</span>
      </div>
      <hm_conmmentItem v-if="item.parent" :commentItem="item.parent" @replay="replayComment"></hm_conmmentItem>
      <div class="text">{{item.content}}</div>
    </div>
    <hm_comment :artive="artive" @refresh="refresh" :commentObj="temp" @cancel="temp=null"></hm_comment>
  </div>
</template>

<script>
import hm_header from "@/components/hm_header.vue";
import hm_conmmentItem from "@/components/hm_conmmentItem.vue";
import { getComment, getPost } from "@/apis/post.js";
import axios from "@/utils/request.js";
import hm_comment from "@/components/hm_comment.vue";
export default {
  components: {
    hm_header,
    hm_conmmentItem,
    hm_comment
  },
  data() {
    return {
      commentList: [],
      axios,
      artive: {},
      temp: {}
    };
  },
  async mounted() {
    // let id = this.$route.params.id;

    // console.log(this.commentList[0].parent);

    this.init();
  },
  methods: {
    async init() {
      let id = this.$route.params.id;

      this.artive = (await getPost(id)).data.data;

      // let res = await getComment(id);
      this.commentList = (await getComment(id)).data.data;
      // console.log(res);

      // this.commentList = res.data.data;
    },
    refresh() {
      this.init();
      window.scrollTo(0, 0);
    },
    replayComment(v) {
      console.log("父组件中的", v);
      this.temp = v;
    }
  }
};
</script>

<style lang="less" scoped>
.item {
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    > img {
      width: 50/360 * 100vw;
      height: 50/360 * 100vw;
      display: block;
      border-radius: 50%;
    }
    > div {
      flex: 1;
      display: flex;
      flex-direction: column;
      margin-left: 10px;
      > span {
        font-size: 12px;
        color: #999;
        line-height: 25px;
      }
    }
    > span {
      color: #999;
      font-size: 13px;
    }
  }
  .text {
    font-size: 14px;
    color: #333;
    padding: 20px 0 10px 0;
  }
}
</style>